using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent(typeof(MeshCollider))]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class Sheet : MonoBehaviour {
	
	private int nbRow, nbCol;
	private enum ANIM_STATE {NONE, TURN_LEFT, TURN_RIGHT};
	private ANIM_STATE currentAnimState;
	private float angle;
	private int achievableHeight;
	private int currentHeight;
	private float pivot_z;
	private float incrementPivot;
    private List<Square> topSquaresList;
    private List<Square> bottomSquaresList;
		
	// Use this for initialization
	void Start () {
		//angle = 0;
	}
	
	public void Build(int nbRows, int nbCols, bool rightPage, int height, Color sheetColor)
	{
		nbRow = nbRows;
		nbCol = nbCols;
        topSquaresList = new List<Square>();
        bottomSquaresList = new List<Square>();
		
		if(!rightPage) angle = -180f;
				currentHeight = height;
		
		for (int i = 0; i < nbRow; ++i)
        {
            for(int j = 0; j < nbCol;++j)
            {
                float f_i = i, f_j = j;
				GameObject newOne = new GameObject();
				/*TagMan
				newOne.tag = "test_"+i*j;*/
				newOne.AddComponent("Square");
				float delta_i = ((float)1) / nbRow;
				float delta_j = ((float)1) / nbCol;
				
				newOne.transform.parent = gameObject.transform;
                
                Square tmp = (Square) newOne.GetComponent("Square");
				tmp.Build(new Vector3(f_i, f_j, 0.0001f), 
					new Vector3(f_i + 1, f_j, 0.0001f), 
					new Vector3(f_i + 1, f_j + 1, 0.0001f), 
					new Vector3(f_i, f_j + 1, 0.0001f),
					
                    new Vector2(f_i * delta_i, f_j * delta_j), 
					new Vector2((f_i + 1) * delta_i, f_j * delta_j), 
                    new Vector2((f_i + 1) * delta_i, (f_j + 1) * delta_j), 
					new Vector2(f_i * delta_i, (f_j + 1) * delta_j),
					      
					"old_paper","test"+i*j);

                tmp.renderer.material.color = sheetColor;

                topSquaresList.Add(tmp);
				
				newOne = new GameObject();
				
				newOne.transform.parent = gameObject.transform;
				newOne.AddComponent("Square");
                
                tmp = (Square) newOne.GetComponent("Square");
				tmp.Build(new Vector3(f_i, f_j, -0.0001f),
					new Vector3(f_i, f_j + 1, -0.0001f), 
					new Vector3(f_i + 1, f_j + 1, -0.0001f), 
					new Vector3(f_i + 1, f_j, -0.0001f),
					
                    new Vector2(f_i * delta_i, f_j * delta_j), 
					new Vector2(f_i * delta_i, (f_j + 1) * delta_j),
                    new Vector2((f_i + 1) * delta_i, (f_j + 1) * delta_j), 
					new Vector2((f_i + 1) * delta_i, f_j * delta_j),
					      
					"old_paper","test"+i*j);
                
                tmp.renderer.material.color = sheetColor;

                bottomSquaresList.Add(tmp);

				
				/*string message = "UV:" + f_i * delta_i + " " + f_j * delta_j;
				Debug.Log(message);
				message = "UV:" + (f_i +1 ) * delta_i + " " + (f_j+1) * delta_j;
				Debug.Log(message);*/
				//gameObject.AddComponent(newOne);

            }
        }
		
	}
	
	public void TurnLeft(int LeftHeight)
	{
		if(currentAnimState == Sheet.ANIM_STATE.NONE)
		{
			currentAnimState = Sheet.ANIM_STATE.TURN_LEFT;
			achievableHeight = LeftHeight;
			pivot_z = transform.position.z;
			incrementPivot = ((float) achievableHeight - currentHeight) / 2000f;
		}
	}
	
	public void TurnRight(int RightHeight)
	{
		if(currentAnimState == Sheet.ANIM_STATE.NONE)
		{
			currentAnimState = Sheet.ANIM_STATE.TURN_RIGHT;
			achievableHeight = RightHeight;
			pivot_z = transform.position.z;
			incrementPivot = ((float) achievableHeight - currentHeight) / 2000f;
		}
	}
	
	public bool isTurning()
	{
		return (currentAnimState != Sheet.ANIM_STATE.NONE);
	}

    public List<Square> getBottomSquaresList()
    {
        return bottomSquaresList;
    }

    public List<Square> getTopSquaresList()
    {
        return topSquaresList;
    }
	
	// Update is called once per frame
	void Update () {
		
		float rotationValue = Time.deltaTime * 200;
		
		switch(currentAnimState)
		{
		case Sheet.ANIM_STATE.NONE:
			break;
			
		case Sheet.ANIM_STATE.TURN_RIGHT:
			if(angle <= -180f)
			{
				angle = -180f;
				currentAnimState = Sheet.ANIM_STATE.NONE;
				currentHeight = achievableHeight;
			}
			else
			{
				angle -= rotationValue;
				if(angle <= -180f)
				{
					angle += rotationValue;
					rotationValue = -(-180f - angle);
					angle = -180f;
				}
				
				pivot_z += incrementPivot;
				
				transform.RotateAround(new Vector3(3f,0f,pivot_z), Vector3.up, -rotationValue);
			}
			break;
			
			
		case Sheet.ANIM_STATE.TURN_LEFT:			
			if(angle >= 0f)
			{
				angle = 0f;
				currentAnimState = Sheet.ANIM_STATE.NONE;
				currentHeight = achievableHeight;
			}
			else
			{
				angle += rotationValue;
				if(angle >= 0f)
				{
					angle -= rotationValue;
					rotationValue = -angle;
					angle = 0f;
				}
				
				pivot_z += incrementPivot;
				
				transform.RotateAround(new Vector3(3f,0f,pivot_z), Vector3.up, rotationValue);
				
			}
			break;
			
		}
	}
}
